﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Engine.Graphics;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Engine.Cameras;

namespace Estadio.GameObjects
{
    public class VideoPanel : UserModel<VertexPosition4Texture, short>
    {
        #region Video Specific

        public bool FullScreen { get; set; }

        /// <summary>
        /// Preserves the aspect ratio of the video.
        /// Only when on full screen mode.
        /// </summary>
        public bool PreserveAspect { get; set; }

        public bool IsLooping { get { return videoPlayer.IsLooped; } set { videoPlayer.IsLooped = value; } }

        Video video;
        VideoPlayer videoPlayer;
        #endregion

        public VideoPanel(Video video)
            : base(null, 4)
        {
            InitializeQuad();

            this.video = video;
            videoPlayer = new VideoPlayer();
            IsLooping = true;

            Reset();
        }



        private void InitializeQuad()
        {
            vertices[0].Position = new Vector4(-1.0f, 1.0f, 1.0f, 1.0f); //Top left. (0)
            vertices[0].TextureCoordinate = new Vector2(0, 0);

            vertices[1].Position = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); //Top right. (1)
            vertices[1].TextureCoordinate = new Vector2(1, 0);

            vertices[3].Position = new Vector4(1.0f, -1.0f, 1.0f, 1.0f); //Bottom right. (3)
            vertices[3].TextureCoordinate = new Vector2(1, 1);

            vertices[2].Position = new Vector4(-1.0f, -1.0f, 1.0f, 1.0f); //Bottom left. (2)
            vertices[2].TextureCoordinate = new Vector2(0, 1);



            CreateVertexBuffer();

            short[] Indices = new short[6];
            Indices[0] = (short)0;
            Indices[1] = (short)3;
            Indices[2] = (short)2;
            Indices[3] = (short)0;
            Indices[4] = (short)1;
            Indices[5] = (short)3;

            CreateIndexBuffer(Indices);

        }

        public void Play()
        {
            if (videoPlayer.State == MediaState.Paused)
                videoPlayer.Resume();
        }

        public void Pause()
        {
            if (videoPlayer.State == MediaState.Playing)
                videoPlayer.Pause();
        }

        public void Stop()
        {
            if (videoPlayer.State != MediaState.Stopped)
                videoPlayer.Stop();

        }

        public void Reset()
        {
            videoPlayer.Play(video);
            videoPlayer.Pause();
        }

        


        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            BasicEffect effect = Effect as BasicEffect;
            effect.Texture = videoPlayer.GetTexture();

            if (FullScreen)
            {

                effect.World = Matrix.Identity;
                effect.View = Matrix.Identity;
                effect.Projection = Matrix.Identity;
                Draw(effect);
            }
            else
                base.Draw(gameTime);

        }
    }
}
